//*************************************************************************************************
//************************ (C) 2001 - 2006, Eleven Engineering Incorporated ***********************
//*************************************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**          File:  Thread1.asm
//**       Project:  Quick Start Project
//**       Created:  25 Jun 2002 by Ryan Northcott
//**       Revised:  08 Sep 2006 by Dan Beutel
//**
//**   Description:  This file contains the application code that will be run by thread 1.
//**
//** Compatibility:  Requires XInA v4.4 or higher.
//**
//**    Disclaimer:  You may incorporate this sample source code into your program(s) without
//**                 restriction.  This sample source code has been provided "AS IS" and the
//**                 responsibility for its operation is yours.  You are not permitted to
//**                 redistribute this sample source code as "Eleven sample source code" after
//**                 having made changes.  If you are going to re-distribute the source, we require
//**                 that you make it clear in the source that the code was descended from Eleven
//**                 sample source code, but that you have made changes.
//**
//*************************************************************************************************
//*************************************************************************************************

			mov   	r7,T1_SP //setting up stack pointer for thread 1

			mov 	r1, 0x0000 //housekeeping, ensures this reg is clean

			jsr 	r6, LCD_Init //initializes the LCD

			mov 	r1, 0b1000111100000011 // old single bit sample code=0b1000100000000000
			//			0b1 111 0 1 1 0 00000000

			//mov 	r1, 0b1111011000000000  //THIS WORKS, but samples 4 channels?
			outp 	r1, ADCcfg0 //initializes the ADC for continuous mode with ThreadLock enabled

			inp		r0, ADCcfg1
			bis		r0, r0, 6			//sets reset bit
			outp	r0, ADCcfg1
			jsr 	r6, DelayLong
			bic		r0, r0, 6			//0's reset bit
			outp	r0, ADCcfg1 	//Initial reset to satisfy ADC poweron condition
			inp		r1, ADCcfg0
			bis 	r1, r1, 9
			outp	r1, ADCcfg0

			mov 	r0, '0'
			jsr 	r6, LCD_Char1
			mov 	r0, 'x'
			jsr 	r6, LCD_Char1 //outputs formatting string

			//jsr r6, Start_ADC
			mov 	r1, 0x0030
			jsr r6, DelayReallyLong
			jsr r6, DelayReallyLong
			jsr r6, DelayReallyLong
			jsr r6, DelayReallyLong
			jsr r6, DelayReallyLong
			jsr r6, DelayReallyLong
			jsr r6, DelayReallyLong

ReadData:
			//add r1, r1, 1
			mov		r0, 0x82 //change from 0x82
			jsr		r6, LCD_Command1 //brings cursor back home

			//get ADC data
			inp 	r1,ADCdata //because of FC being set, this will only finish when reading is ready. R1 = untouched ADC data


			//getting MSB
			mov	 	r2,r1

			rol 	r2,r2,-8 //now have MSB of 3 char adc output in r2
			and		r2,r2,0x0003 //zeroes out all of the bits except two MSB marcinchange from 0x0300

			//convert and output MSB
			mov 	r3,r2
			jsr 	r6,hex2ascii  //gives us MSB
			mov 	r0,r3
			jsr 	r6, LCD_Char1

			//gets second MSB
			mov 	r2,r1

			rol 	r2,r2,-4
			and 	r2,r2,0x000F

			//convert and output second msb
			mov 	r3,r2
			jsr 	r6,hex2ascii
			mov 	r0,r3
			jsr 	r6, LCD_Char1

			//gets LSB
			mov 	r2,r1
			and 	r2,r2,0x000F

			//convert and output MSB
			mov 	r3,r2
			jsr 	r6,hex2ascii
			mov 	r0,r3
			jsr 	r6, LCD_Char1

//Finished outputting the data, wait a bit start again
			jsr		r6, DelayReallyLong
			jsr		r6, DelayReallyLong
			//mov 	r0, 0x01		// return cursor home established this works but turns sreen off?
			//jsr 	r6, LCD_Command1
			//jsr		r6, DelayLong
			//mov		r0, 0b10000000 //should return display to home and clear
			//jsr 	r6, LCD_Command1
			//jsr		r6, DelayLong
			bra 	ReadData

	bra	@	// Prevent this thread from running away if it is accidentally enabled in the main file

//=============================================================================
// Input Params:	hex value in r3
// Output Params:	ascii value in r3
//-----------------------------------------------------------------------------
// Description: converts value in r3 into the hex value of ascii of r3
// 			**ONLY VALID FOR NUMBERS, Does not check for valid input**
//=============================================================================
hex2ascii:
			st	r1, sp, 0 //housekeeping

			mov r1, r3
			add r1, r1, -9
			//bra zerotonine
			bc LE0, zerotonine //if the digit is less than 9 we need different offset

			add r3,r3,0x37 // will give us A-F
			bra finishedcon
zerotonine:
			add r3,r3,0x30 // will give us 0-9

finishedcon:
			ld	r1, sp, 0 //housekeeping
			jsr	r6, r6

